#!/bin/bash
# chkconfig:2345 11 90
# description: Docker zookeeper

BEGIN=100
END=100
Name=zoo
Host="$Name"
Mem=2048m
C_ZOO_DATA_DIR="/opt/zoo/data"
C_ZOO_DATALOG_DIR="/opt/zoo/datalog"
C_ZOO_LOGS_DIR="/opt/zoo/logs"
C_ZOO_CONF="/opt/zoo/conf/zoo.cfg"
C_ZOO_MYID="/opt/zoo/data/myid"

H_ZOO_DATA_DIR="/opt/docker/zoo/data"
H_ZOO_DATALOG_DIR="/opt/docker/zoo/datalog"
H_ZOO_LOGS_DIR="/opt/docker/zoo/logs"
H_ZOO_CONF="/opt/docker/zoo/zoo.cfg"
H_ZOO_MYID="/opt/docker/zoo/myid"



#IOS="192.168.0.136:5000/singulax/zookeeper:3.4.6"
IOS="singulax/activemq:3.4.6"

Net="172.17.4"
GATEWAY="$Net.254"



create(){
   for ((n=BEGIN;n<=END;n++))
   do
     docker  run -d --privileged --name $Name$n -h $Host$n -m $Mem --memory-swap=$Mem -v /etc/localtime:/etc/localtime -v $H_ZOO_MYID:$C_ZOO_MYID -v $H_ZOO_CONF:$C_ZOO_CONF -v $H_ZOO_LOGS_DIR:$C_ZOO_LOGS_DIR -v $H_ZOO_DATA_DIR:$C_ZOO_DATA_DIR -v $H_ZOO_DATALOG_DIR:$C_ZOO_DATALOG_DIR     $IOS
    /usr/local/bin/pipework  docker0  -i  eth1  $Name$n $Net.$n/24@$GATEWAY
    docker  exec   -it   $Name$n  ifconfig  eth0  down
    docker  exec   -it   $Name$n  route  add  default  gw  $GATEWAY
   done
}
delete(){
  for ((n=BEGIN;n<=END;n++))
  do
   docker  stop   $Name$n  &&  docker  rm  $Name$n
  done
}
start(){
  for ((n=BEGIN;n<=END;n++))
  do
    docker  start   $Name$n
    /usr/local/bin/pipework docker0 -i eth1  $Name$n  $Net.$n/24@$GATEWAY
    docker  exec   -it   $Name$n  ifconfig  eth0  down
    docker  exec   -it   $Name$n  route  add  default  gw  $GATEWAY
  done
}
stop(){
  for ((n=BEGIN;n<=END;n++))
  do
   docker  stop   $Name$n
  done
}
case $1 in
create)
   echo "Creating  $Name  Containner  for  $Name Docker Service"
   create
;;
delete)
   echo "Deleting  $Name  Containner  for  $Name Docker Service"
   delete
;;
start)
   echo  "Start running  $Name Containner"
   start
;;
stop)
   echo  "Stop running   $Name Containner"
   stop
;;
*)
   echo  $"Usage: $0 {start|stop|create|delete}"
   exit 1
;;
esac
